$(function() {
	$.post(getUrlBySuffix("/menuAdmin/getAllRole"), {}, function(data) {
		var list = eval(data);
		var _html = ""
		for (var i = 0; i < list.length; i++) {
			var _temp = "<option value='" + list[i].id + "' roleName='"
					+ list[i].name + "' data-tokens='" + list[i].descriptor
					+ "'>" + list[i].descriptor + "</option>"
			_html += _temp;
		}

		$("#roleList").attr("data-live-search", "true");
		$("#roleList").append(_html);
		$("#roleList").selectpicker('refresh');
	})
  
	$("#roleList").on("change", function() {
		var thisRodeId = $(this).find("option:selected").val();
		$("input[name=roleId]").val(thisRodeId);
		var roleID = $("input[name=roleId]").val();
		if(roleID==""){
    		swal("提示", "请选择一个角色");
    		return false;
    	}
		var param = {
			roleId : Number(roleID)
		}
		$.post(getUrlBySuffix("/menuAdmin/findMenuRoleByRoleId"),
				param, function(json) {
					var list = eval(json["list"]);
					if(!(list==null)){
						for (var i = 0; i < list.length; i++) {
							$("#menuRoleCheckBox_No" + list[i].menuId + "").attr("checked", true);
							$("#menuRoleFunctionDetail_No" + list[i].menuId + "").val(list[i].functionDetail);
						}
					}
					
				})
	});
	
	
    
	// 一级菜单总数量
	var menuRoleSize = $("#menuRoleSize").val();
	roleAllSelectCheckBox();
	roleAllGiveUpCheckBox();
	menuRoleAllSelectCheckBox(menuRoleSize);
	clickSearchButton();
	clickConfirm();
	updateButtonInit();
})
/*点击编辑按钮*/
function updateButtonInit(){
	
    $(".update-btn").on("click", function () {
    	var thisFunctionId=$(this).siblings(".menuRoleCheckBox").attr("functionId");
    	var thisFunctionDetail=$(this).siblings(".menuRoleFunctionDetail").val();
    	var thisMenuCheckBoxId=$(this).siblings(".menuRoleFunctionDetail").attr("id");
    	$("input[name=functionDetailTableId]").val(thisMenuCheckBoxId);
    	var roleId=$("input[name=roleId]").val();
    	if(roleId==""){
    		swal("提示", "请选择一个角色");
    		return false;
    	}
    	var params={functionId:thisFunctionId,roleId:roleId}
    	$.post(getUrlBySuffix("/menuAdmin/getRoleFunction"),params,function(data){
    		var list = data.list;
    		showRoleFunctionList(list,"editForm",thisFunctionDetail,thisMenuCheckBoxId);
    	})
    	
        $("#role-menu-functions-edit-modal").modal("show");//显示任务编辑模态框
    });
    
	$("#commitfuncDBtn").on("click",function(){
		var checkedBoxs=$("input[name=chooseFunction]:checked");
		var targetFunDetailPostion=$("input[name=functionDetailTableId]").val();
		if(checkedBoxs.length==0){
			swal("提示", "未选择任何一个功能");
			return false;
		}
		var functionDetail="[";
		for(var i=0;i<checkedBoxs.length;i++){
			var checkedBox=checkedBoxs[i];
			var functionId=$(checkedBox).siblings("input[name=functionId]").val();
			

			var functionName=$(checkedBox).parent().siblings(".functionName").html();
			var functionUrl=$(checkedBox).parent().siblings(".functionUrl").html();
			var functionAlais=$(checkedBox).parent().siblings(".functionAliasTd").find("input[name=functionAlias]").val();
			if(functionAlais==""){
				$(checkedBox).parent(".functionAliasTd").find("input[name=functionAlias]").css("border-color","red");
				swal("提示", functionName+"该功能的别名未写");
				$(checkedBox).parent(".functionAliasTd").find("input[name=functionAlias]").foucs();
				return false;
			}
			var functionHt=$(checkedBox).parent().siblings(".functionHandle").find("input[name=functionHandleType]").val()
			var functionIcon=$(checkedBox).parent().siblings(".iconValue").find("input[name=functionIcon]").val();
			var functionEle="{'functionId':'"+functionId+"','functionName':'"+functionName+"','functionUrl':'"+functionUrl+"','functionAlais':'"+functionAlais+"','functionIcon':'"+functionIcon+"','functionHt':'"+functionHt+"'},";
			functionDetail+=functionEle;
		}
		functionDetail=functionDetail.substring(0,functionDetail.length-1);
		functionDetail+="]";
		$("#"+targetFunDetailPostion).val(functionDetail);
		swal("提示", "未选择任何一个功能");
		swal({
			title : "提示",
			text : "编辑成功",
			type : "success",
			confirmButtonText : "确定"
		}, function() {
			$("#role-menu-functions-edit-modal").modal("hide");
		});
	})
}

/**
 * 点击搜索
 * 
 */
function clickSearchButton() {
	$(document).on(
			"click",
			"#searchBtn",
			function() {
				var roleID = $("input[name=roleId]").val();
				if(roleID==""){
		    		swal("提示", "请选择一个角色");
		    		return false;
		    	}
				var param = {
					roleId : Number(roleID)
				}
				$.post(getUrlBySuffix("/menuAdmin/findMenuRoleByRoleId"),
						param, function(json) {
							var list = eval(json["list"]);
							if(!(list==null)){
								for (var i = 0; i < list.length; i++) {
									$("#menuRoleCheckBox_No" + list[i].menuId + "").attr("checked", true);
									$("#menuRoleFunctionDetail_No" + list[i].menuId + "").val(list[i].functionDetail);
								}
							}
							
						})
			});
}
/*
 * 点击提交
 */
function clickConfirm() {
	$("#commitBtn").on(
			"click",
			function() {
				if ($("input[class=menuRoleCheckBox]:checked").length == 0) {
					swal("提示", "请选择一个菜单");
					return false;
				}
				if ($("input[name=roleId]").val() == "") {
					swal("提示", "请选择一个角色");
					return false;
				}
				var params = {
					menuIds : "["+requestData()+"]",
					roleId : Number($("input[name=roleId]").val())
				}
				console.log(params);
				$.post(getUrlBySuffix("/menuAdmin/insertMenuRoleList"), params,
						function(json) {
							if (json.success) {
								swal({
									title : "提示",
									text : json['message'],
									type : "success",
									confirmButtonText : "确定"
								}, function() {
									location.reload();
								});
							} else {
								swal({
									title : "提示",
									text : json['message'],
									type : "error"
								});
							}
						})

			})
}
/**
 * 获取被选中的复选框的value值
 * 
 * @param checkArray
 */
function requestData() {
	var checkArray = new Array();
	$('input[class="menuRoleCheckBox"]:checked').each(function() {
		
		var menuId=$(this).val();
		var functionDetail=$(this).siblings("input[class=menuRoleFunctionDetail]").val();
		checkArray.push('{"menuId":'+menuId+',"functionDetail":'+(functionDetail==""?"\"\"":functionDetail)+'}');
	})
	return checkArray;
}
/**
 * 全选 checkBox
 */
function roleAllSelectCheckBox() {
	$("#roleAllSelectCheckBox").click(function() {
		$.each($(".menuRoleCheckBox"), function() {
			this.checked = true;
		})
	});
}

/**
 * 全不选
 */
function roleAllGiveUpCheckBox() {
	$("#roleAllGiveUpCheckBox").click(function() {
		$.each($(".menuRoleCheckBox"), function() {
			this.checked = false;
		})
	});
}

function menuRoleAllSelectCheckBox(menuRoleSize) {
	for (var i = 1; i <= menuRoleSize; i++) {
		$("#selectCheckBox_" + i).click(function() {
			$.each($(this).parent().find(".menuRoleCheckBox"), function() {
				this.checked = true;
			});
		});
		$("#giveUpCheckBox_" + i).click(function() {
			$.each($(this).parent().find(".menuRoleCheckBox"), function() {
				this.checked = false;
			});
		});
	}
}

function showRoleFunctionList(list,elementId,chosenList,thisMenuCheckBoxId){
	var _html="";
	for(var i=0;i<list.length;i++){
		var _td="<tr>" +
				"<td>"+
					"<input value='' type='checkBox' name='chooseFunction' class='chooseFunction' id='chooseFunction"+list[i].tfid+"' />" +
					"<input value='"+list[i].tfid+"' type='hidden' name='functionId' class='functionId' />" +
				"</td>" +
				"<td class='functionName'>"+list[i].name+"</td>" +
				"<td class='functionUrl'>"+list[i].url+"</td>" +
				"<td>"+(list[i].methodType==0?'GET':'POST')+"</td>" +
				"<td class='functionAliasTd'><input value='' type='text' name='functionAlias' class='functionAlias form-control' placeholder='请输入别名'/></td>"+
				"<td class='functionHandle'>" +
					"<input  value='' type='text' name='functionHandleType' class='functionHandleType form-control' placeholder='请输入操作类型'/>" +
				"</td>"+
				"<td class='functionHandleSelect'><select class='selectpicker handleTypeSelect' data-width='100px'><option selected='selected'>操作类型</option></select></td>" +
				"<td class='iconPostionTd'><span class='icon-postion' style='margin-top: 1%;position: absolute;'/></span></td>" +
				"<td class='iconValue'>" +
					"<input value='' type='hidden' name='functionIcon' class='functionIcon'/>" +
					"<select class='selectpicker iconSelect' data-width='100px'><option selected='selected'>图标选择</option></select>"+
					"</td></tr>";
		_html+=_td;
	}
	$("#"+elementId).find("tbody").html(_html);
	$("#"+elementId).find("input[name=thisMenuCheckBoxId]").val(thisMenuCheckBoxId);
	if(!(chosenList=="")){
		var _list=eval(chosenList);
		if(_list.length>0){
			for(var i=0;i<_list.length;i++){
				console.log(_list[i]);
				$("#chooseFunction"+_list[i].functionId).attr("checked","checked");
				$("#chooseFunction"+_list[i].functionId).parent().siblings(".functionAliasTd").find("input[name=functionAlias]").val(_list[i].functionAlais);
				$("#chooseFunction"+_list[i].functionId).parent().siblings(".iconValue").find("input[name=functionIcon]").val(_list[i].functionIcon);
				$("#chooseFunction"+_list[i].functionId).parent("td").siblings(".iconPostionTd").find(".icon-postion").html("<i class='fa "+_list[i].functionIcon+"'></i>");
				$("#chooseFunction"+_list[i].functionId).parent("td").siblings(".functionHandle").find("input[name=functionHandleType]").val(_list[i].functionHt);
			}
		}
	}
	
	
	initIconSelect();
}

function initIconSelect(){
	var _options="";
	for(var key in iconList){
		var _tempOption="<option value='" + iconList[key] + "'>"+key+"</option>";
		_options+=_tempOption;
	}
	
	
	$(".iconSelect").each(function(){
		$(this).append(_options);
		$(this).selectpicker('refresh');
	})
	
	var _handleHtml="";
	for(var handleKey in handleTypeList){
		var _tempHandle="<option value='" + handleTypeList[handleKey] + "'>"+handleKey+"</option>";
		_handleHtml+=_tempHandle;
	}
	$(".handleTypeSelect").each(function(){
		$(this).append(_handleHtml);
		$(this).selectpicker('refresh');
	})
	
	$(".iconSelect").on("change",function(){
		var thisIconValue = $(this).find("option:selected").val();
		$(this).parent("td").siblings(".iconPostionTd").find(".icon-postion").html("<i class='fa "+thisIconValue+"'></i>");
		$(this).siblings(".functionIcon").val(thisIconValue);
	})
	$(".handleTypeSelect").on("change",function(){
		var thisHandleTypeValue = $(this).find("option:selected").val();
		$(this).parent("td").siblings(".functionHandle").find("input[name=functionHandleType]").val(thisHandleTypeValue);
	})
	
	
}